package com.market.web.model;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import com.market.domain.BaseObject;
import com.market.domain.User;
import com.market.domain.UserActivityRecord;
import com.market.web.controller.PasswordRecoverController;

@Component("userModel")
@Scope("session")
public class UserModel extends GenericModel<User> {

	

	public List<SelectItem> getAvailableLanguages() {
		List<SelectItem> languages = new ArrayList<SelectItem>();
		for (User.LANGUAGE language : User.LANGUAGE.values()) {
			languages.add(new SelectItem(language, language.getLabel()));
		}
		return languages;
	}

	public List<SelectItem> getAvailableTitles() {
		List<SelectItem> titles = new ArrayList<SelectItem>();
		for (User.TITLE title : User.TITLE.values()) {
			titles.add(new SelectItem(title, title.getLabel()));
		}
		return titles;
	}

	@Override
	public User getNewObject() {
		User user = new User();
		user.setIsNew(true);
		return user;
	}

	public User getAuthenticatedUser() {
		Authentication au = SecurityContextHolder.getContext()
				.getAuthentication();

		if (au != null) {
			Object obj = au.getPrincipal();
			if (obj instanceof User) {
				User user = (User) obj;
				user.setIsLogin(true);
				// this.setCurrent(user);
				return user;
			}
		}
		return null;
	}
}
